﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Wheeler.Web.ViewModels;
using Wheeler.Domain;

namespace Wheeler.Web.Utility
{
    public class AppRoleProvider : RoleProvider
    {
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            if (SessionCache.CurrentUser != null)
            {
                List<GroupViewModel> groups = ApplicationCache.UserGroups;
                return (from P in groups where P.ID == SessionCache.CurrentUser.GroupID select P.Name).ToArray();
            }
            return new String[] {};
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            WheelerEntities context = new WheelerEntities();
            var user = context.Users.Include("Group").FirstOrDefault(P => String.Compare(P.UserName, username, true) == 0);
            if (user == null)
                return false;
            else
            {
                List<GroupViewModel> groups = ApplicationCache.UserGroups;
                if (groups.FirstOrDefault(P => String.Compare(P.Name, user.Group.Name, true) == 0) == null)
                    return false;

                return true;
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}